#include <project.h>
#include "userMain.h"
#include "userLedDriver.h"

#define	    LED_LEVEL_HIGH_MAX		250
#define	    LED_LEVEL_HIGH_MIN		150
#define	    LED_LEVEL_HIGH_MIN_L    50

#define 	LED_LEVEL_LOW_MAX	125
#define		LED_LEVEL_LOW_MIN	75
#define		LED_LEVEL_LOW_MIN_L	25

extern unsigned char userAlarmFlag;

unsigned char userLedLevelMax=0;
unsigned char userLedLevelMin=0;
unsigned char userLedLevelMinL = 0;
void userLedInitFunc(void)
{
    LED_Write(0x00);
    PWM_Local_Start();
    PWM_Distict_Start();
    userBacklightModifyFunc(LED_LEVEL_HIGH_MIN);
    userLedLevelSetFunc(1);
}

unsigned char userLedLevel = 1;
unsigned char userLedValueNow= 0;
unsigned char uDistictLedFlag = 0;
unsigned char uDistictLedCnt = 2;
void userLedLevelSetFunc(unsigned char ledLevel)
{
	if(ledLevel)
	{
		userLedLevel = 1;
		userLedLevelMax=LED_LEVEL_HIGH_MAX;
		userLedLevelMin=LED_LEVEL_HIGH_MIN;
        userLedLevelMinL = LED_LEVEL_HIGH_MIN_L;
	}
	else
	{
		userLedLevel = 0;
		userLedLevelMax=LED_LEVEL_LOW_MAX;
		userLedLevelMin=LED_LEVEL_LOW_MIN;
         userLedLevelMinL = LED_LEVEL_LOW_MIN_L;
	}
    userBackLedSetFunc(0xFF);///////////////////////////////////////
}
void userBacklightModifyFunc(unsigned char Value) //背光灯调光
{
    userLedValueNow = Value;
    userDistictLedModify(userLedValueNow);
    userLocalLedModify(userLedValueNow);
}

void userDistictLedModify(unsigned char ledValue)
{
    unsigned int userValue;
    userValue=ledValue*255;
    PWM_Distict_Start();
    PWM_Distict_WriteCompare(userValue);
}
void userLocalLedModify(unsigned char ledValue)
{
    unsigned int userValue;
    userValue=ledValue*255; 
    PWM_Local_Start();
    PWM_Local_WriteCompare(userValue);
}

unsigned char userLedBlinkFlag = 0;
unsigned char userLedStateTemp=0;
unsigned char userLedState=0;
unsigned char userLedStatePre=0;
unsigned char userBlinkCnt=0;

void userLedCtrlFunc(unsigned char userLedIndex, unsigned char userOperateMode)
{
    unsigned char userVar;
    if(userLedIndex > 9)return ;
    if(userLedIndex == 9) 
    {
       userLedLevelSetFunc(userOperateMode);
        return ;
    }
    userLedStateTemp = userLedState;
    switch(userLedIndex)
    {
        case 0x01:
        {
            if(userOperateMode == 1)
            {
                userLedStateTemp |= 0x10;
                userLedBlinkFlag &= 0xEF;
            }
            if(userOperateMode == 0)
            {
                userLedStateTemp &= 0xEF;
                userLedBlinkFlag &= 0xEF;
            }
            if(userOperateMode == 4)
            {
                userLedStateTemp &= 0xEF;
                userLedBlinkFlag |= 0x10;
            }
            break;
        }
        case 0x02:
        {
            if(userOperateMode == 1)
            {
                userLedStateTemp |= 0x20;
                userLedBlinkFlag &= 0xDF;
            }
            if(userOperateMode == 0)
            {
                userLedStateTemp &= 0xDF;
                userLedBlinkFlag &= 0xDF;
            }
            if(userOperateMode == 4)
            {
                userLedStateTemp &= 0xDF;
                userLedBlinkFlag |= 0x20;
            }
            break;
        }
        case 0x03:
        {
            if(userOperateMode == 1)
            {
                userLedStateTemp |= 0x40;
                userLedBlinkFlag &= 0xBF;
            }
            if(userOperateMode == 0)
            {
                userLedStateTemp &= 0xBF;
                userLedBlinkFlag &= 0xBF;
            }
            if(userOperateMode == 4)
            {
                userLedStateTemp &= 0xBF;
                userLedBlinkFlag |= 0x40;
            }
            break;
        }
        case 0x04:
        {
            if(userOperateMode == 1)
            {
                userLedStateTemp |= 0x80;
                userLedBlinkFlag &= 0x7F;
            }
            if(userOperateMode == 0)
            {
                userLedStateTemp &= 0x7F;
                userLedBlinkFlag &= 0x7F;
            }
            if(userOperateMode == 4)
            {
                userLedStateTemp &= 0x7F;
                userLedBlinkFlag |= 0x80;
            }
            break;
        }
        case 0x05:
        {
            if(userOperateMode == 1)
            {
                userLedStateTemp |= 0x01;
                userLedBlinkFlag &= 0xFE;
            }
            if(userOperateMode == 0)
            {
                userLedStateTemp &= 0xFE;
                userLedBlinkFlag &= 0xFE;
            }
            if(userOperateMode == 3)
            {
                userLedBlinkFlag &= 0xFE;
                userVar = userLedStateTemp & 0x01;
                userVar = userVar ^ 0x01;
                userLedStateTemp &= 0xFE;
                userLedStateTemp += userVar;
            }
            if(userOperateMode == 4)
            {
                userLedStateTemp &= 0xFE;
                userLedBlinkFlag |= 0x01;
            }
            break;
        }
        case 0x06:
        {
            if(userOperateMode == 1)
            {
                userLedStateTemp |= 0x02;
                userLedBlinkFlag &= 0xFD;
            }
            if(userOperateMode == 0)
            {
                userLedStateTemp &= 0xFD;
                userLedBlinkFlag &= 0xFD;
            }
            if(userOperateMode == 3)
            {
                userLedBlinkFlag &= 0xFD;
                userVar = userLedStateTemp & 0x02;
                userVar = userVar ^ 0x02;
                userLedStateTemp &= 0xFD;
                userLedStateTemp += userVar;
            }
            if(userOperateMode == 4)
            {
                userLedStateTemp &= 0xFD;
                userLedBlinkFlag |= 0x02;
            }
            break;
        }
        case 0x07:
        {
            if(userOperateMode == 1)
            {
                userLedStateTemp |= 0x04;
                userLedBlinkFlag &= 0xFB;
            }
            if(userOperateMode == 0)
            {
                userLedStateTemp &= 0xFB;
                userLedBlinkFlag &= 0xFB;
            }
            if(userOperateMode == 3)
            {
                userLedBlinkFlag &= 0xFB;
                userVar = userLedStateTemp & 0x04;
                userVar = userVar ^ 0x04;
                userLedStateTemp &= 0xFB;
                userLedStateTemp += userVar;
            }
            if(userOperateMode == 4)
            {
                userLedStateTemp &= 0xFB;
                userLedBlinkFlag |= 0x04;
            }
            break;
        }
        case 0x08:
        {
            if(userOperateMode == 1)
            {
                userLedStateTemp |= 0x08;
                userLedBlinkFlag &= 0xF7;
            }
            if(userOperateMode == 0)
            {
                userLedStateTemp &= 0xF7;
                userLedBlinkFlag |= 0xF7;
            }
            if(userOperateMode == 4)
            {
                userLedStateTemp &= 0xF7;
                userLedBlinkFlag |= 0x08;
            }
            break;
        }
         case 0xFF:
        {
            if(userOperateMode == 1)
            {
                userLedStateTemp = 0x00;
                userLedBlinkFlag = 0x00;
            }
            if(userOperateMode == 0)
            {
                userLedStateTemp = 0xFF;
                userLedBlinkFlag = 0x00;
            }
            if(userOperateMode == 4)
            {
                userLedStateTemp = 0x00;
                userLedBlinkFlag = 0xFF;
            }
            break;
        }
        default:
        {
            break;
        }
    }
    userLedState = userLedStateTemp;
    LED_Write(userLedStateTemp);
}

unsigned int userLedMarkTick = 0;
unsigned int userLedMarkTickA = 0;
unsigned int userLedMarkTickB = 0;
void userLedTickFunc(void)
{
    if(userLedMarkTick < 5000)
    {
        userLedMarkTick++;
    }
    if(userLedMarkTickA < 5000)
    {
        userLedMarkTickA++;
    }
    if(userLedMarkTickB < 5000)
    {
        userLedMarkTickB++;
    }
}

unsigned char userStateChangeFlag = 0;
unsigned char userNetLedFlag = 0;
extern unsigned char userCurrentNetState;
unsigned char userLedBlinkState = 0;
void userLedDisplay(void)
{
    if(userAlarmFlag) 
    {
        userAlarmProcessFunc();
        return ;
    }
    userLedBlinkFunc();
    userDistictLedBlinkFunc();
    switch(userCurrentNetState)
    {
        case 0x01:
        {
            userDistictLedBlinkA();
            userNetLedFlag = 1;
            break;
        }
        case 0x02:
        {
            userDistictLedBlinkB();
            userNetLedFlag = 1;
            break;
        }
        case 0x03:
        {
            if(uDistictLedFlag==0)
            userDistictLedOff();
            userNetLedFlag = 0;
            break;
        }
        default:
        {
            userNetLedFlag = 0;
            if(uDistictLedFlag==0)
            userDistictLedOn();
            break;
        }
    }
}

void userLedProcessFunc(void)
{
   /*********** if(userLedBlinkFlag==0)
    {
        if((userLedState&0xf0) != (userLedStateTemp&0xf0))
        {
            userStateChangeFlag = 1;
        }
        if(userNetLedFlag)
        {
            userLedStateTemp &= 0x0F;
            userLedStateTemp |= userLedBlinkState & 0xf0;
        }
        userLedState = userLedStateTemp;
        CyDelay(5);
        LED_Write(userLedStateTemp);
        CyDelay(5);
        return ;
    }********************/
}



void userLedBlinkFunc(void)
{
    unsigned char userVar;
    if(userLedMarkTick<1000)return ;
    userLedMarkTick = 0;
    if(userLedBlinkFlag == 0x00) return ;
    if(userLedBlinkFlag == 0x7F)
    {
        userLedState = userLedStateTemp ^ userLedBlinkFlag;
        LED_Write(userLedState);
        return ;
    }
    userVar = userLedStateTemp & userLedBlinkFlag;
    userVar = userVar ^ userLedBlinkFlag;
    userLedStateTemp = userLedStateTemp & (~userLedBlinkFlag);
    userLedStateTemp += userVar;
    LED_Write(userLedStateTemp);
}

extern uint8 userBeepEnable;
uint8 userLedStartFlag = 1;
void userLedSelfTestFunc(void)
{
    static uint8 userTestStep = 0;
    uint8 uTestLedState=0;
    if(userLedStartFlag==0)return ;
    switch(userTestStep)
    {
        case 0x00:
        {
            if(userBeepEnable)
                userBeepOn();
            userLedMarkTick = 0;
            userTestStep = 0x01;
            break;
        }
        case 0x01:
        {
            if(userLedMarkTick<200)return ;
            userLedMarkTick = 0;
            if(userBeepEnable)
                userBeepOff();
            uTestLedState = userLedState & 0x0F;
            LED_Write(uTestLedState);
            userTestStep = 0x02;
            break;
        }
        case 0x02:
        {
            if(userLedMarkTick<200)return ;
            userLedMarkTick = 0;
            //if(userBeepEnable)
            //    userBeepOff();
            uTestLedState = userLedState & 0x0F;
            uTestLedState |= 0x10;
            LED_Write(uTestLedState);
            userTestStep = 0x03;
            break;
        }
        case 0x03:
        {
            if(userLedMarkTick<500)return ;
                userLedMarkTick = 0;
            uTestLedState = userLedState & 0x0F;
            uTestLedState |= 0x30;
            LED_Write(uTestLedState);
            userTestStep = 0x04;
            break;
        }
        case 0x04:
        {
           if(userLedMarkTick<500)return ;
                userLedMarkTick = 0;
            uTestLedState = userLedState & 0x0F;
            uTestLedState |= 0x70;
            LED_Write(uTestLedState);    
            userTestStep = 0x05;
            break;
        }
        case 0x05:
        {
            if(userLedMarkTick<500)return ;
                userLedMarkTick = 0;
            uTestLedState = userLedState & 0x0F;
            uTestLedState |= 0xF0;
            LED_Write(uTestLedState);
            userTestStep = 0x06;
            break;
        }
        case 0x06:
        {
            if(userLedMarkTick<500)return ;
                userLedMarkTick = 0;
            uTestLedState = userLedState & 0x0F;
            LED_Write(uTestLedState);
            userTestStep = 0x07;
            break;
        }
        case 0x07:
        {
            if(userLedMarkTick<500)return ;
                userLedMarkTick = 0;
            uTestLedState = userLedState & 0x0F;
            uTestLedState |= 0xF0;
            LED_Write(uTestLedState);
            userTestStep = 0x08;
            break;
        }
        case 0x08:
        {
            if(userLedMarkTick<500)return ;
                userLedMarkTick = 0;
            uTestLedState = userLedState & 0x0F;
            LED_Write(uTestLedState);
            userTestStep = 0x09;
            break;
        }
        case 0x09:
        {
            if(userLedMarkTick<500)return ;
            userLedMarkTick = 0;
            if(userBeepEnable)
                userBeepOn();
            userTestStep = 0x0A;
            break;
        }
        case 0x0A:
        {
            if(userLedMarkTick<200)return ;
            userLedMarkTick = 0;
            if(userBeepEnable)
                userBeepOff();
            userTestStep = 0x0B;
            break;
        }
        case 0x0B:
        {
            if(userLedMarkTick<200)return ;
            userLedMarkTick = 0;
            if(userBeepEnable)
                userBeepOn();
            userTestStep = 0x0C;
            break;
        }
        case 0x0C:
        {
            if(userLedMarkTick<200)return ;
            userLedMarkTick = 0;
            if(userBeepEnable)
                userBeepOff();
            userTestStep = 0x0D;
            userLedStartFlag = 0;
            break;
        }
        default: break;
    }
    
}

void userDistictLedBlinkA(void)
{
    static unsigned char userVal = 0;
    if(userLedMarkTick<1000)return ;
    userLedMarkTick = 0;
    userLedBlinkState = userLedState & 0x0f;
    userLedBlinkState |= 0x10<<userVal;
    LED_Write(userLedBlinkState);
    userVal++;
    if(userVal >=4)
    userVal = 0;
}

void userDistictLedBlinkB(void)
{
    static unsigned char userLedBlinkBFlag = 0;
    //unsigned char userLedBlinkState = 0;
    if(userLedMarkTick<1000)return ;
    userLedMarkTick = 0;
    if(userLedBlinkBFlag)
    {
        userLedBlinkBFlag=0;
        userLedBlinkState &= 0x0f;
    }
    else
    {
        userLedBlinkBFlag=1;
        userLedBlinkState |= 0xf0;
    }
    LED_Write(userLedBlinkState);
}

void userDistictLedOff(void)
{
    userDistictLedModify(0x00);
}

void userDistictLedOn(void)
{
   userDistictLedModify(userLedValueNow);
}

void userDistictLedOperate(unsigned char uCnt)
{
    uDistictLedFlag = 1;
    uDistictLedCnt = uCnt;
    userLedMarkTickB = 0;
}

void userDistictLedBlinkFunc(void)
{
    static unsigned char uDFlag=1;
    if(uDistictLedFlag==0) return ; 
    if(userLedMarkTickB<500)return ;
    userLedMarkTickB = 0;
    if(uDistictLedFlag)
    {
        if(uDFlag)
        {
            if(userCurrentNetState != 0x03)
                userDistictLedOff();
             else
                userDistictLedOn();
            uDFlag = 0;
        }
        else
        {
            if(userCurrentNetState != 0x03)
                userDistictLedOn();
             else
                userDistictLedOff();
            uDFlag = 1;
            uDistictLedCnt--;
            if(uDistictLedCnt==0)
            {
                uDistictLedFlag = 0;     
            }
        }
    }
}

unsigned char uLoadDelayCloseFlag=0;
uint8 userCloseDelayTime = 30;
static unsigned char uCloseCntA = 0;
static unsigned char uCloseCntB = 0;
static unsigned char uCloseCntC = 0;
void userLoadDelayCloseSetFunc(uint8 uDelayFlag)
{
    if(uDelayFlag==1) 
    {
        uLoadDelayCloseFlag |= 0x01;
        uCloseCntA = 0;
    }
    if(uDelayFlag==2) 
    {
        uLoadDelayCloseFlag |= 0x02;
        uCloseCntB = 0;
    }
    if(uDelayFlag==3) 
    {
        uLoadDelayCloseFlag |= 0x04;
        uCloseCntC = 0;
    }
}

void userLoadDelayCloseFunc(void)
{
    if(uLoadDelayCloseFlag==0) return ;
    if(userLedMarkTickA<1000)return ;
    userLedMarkTickA = 0;
    uCloseCntA++;
    uCloseCntB++;
    uCloseCntC++;
    if(uCloseCntA > userCloseDelayTime)
    {
        if(uLoadDelayCloseFlag & 0x01)
        {
            userLedCtrlFunc(5,0);   //Back LED control
            //userLedProcessFunc();
            userRelayCtrlFunc(2,0x2B); 
            //userSendKeyInfoFunc();
            userBeepSetFunc(1,1);
            uLoadDelayCloseFlag &= 0xFE;
            //userSendNodeRegisterInfoFunc();
            //userSendRelayInfoFunc();
        }
    }
    if(uCloseCntB > userCloseDelayTime)
    {
        if(uLoadDelayCloseFlag & 0x02)
        {
            userLedCtrlFunc(6,0);
            userLedProcessFunc();
            userRelayCtrlFunc(2,0x2E);
            //userSendKeyInfoFunc();
            userBeepSetFunc(1,1);
            uLoadDelayCloseFlag &= 0xFD;
            //userSendNodeRegisterInfoFunc();
            //userSendRelayInfoFunc();
        }
    }
    if(uCloseCntC > userCloseDelayTime)
    {
        if(uLoadDelayCloseFlag & 0x04)
        {
            userLedCtrlFunc(7,0);        
            userLedProcessFunc();
            userRelayCtrlFunc(2,0x3A);
            //userSendKeyInfoFunc();
            userBeepSetFunc(1,1);
            uLoadDelayCloseFlag &= 0xFB;
            //userSendNodeRegisterInfoFunc();
            //userSendRelayInfoFunc();
        }
    }
}

void userAlarmProcessFunc(void)
{
    if(userAlarmFlag==0)
        return ;
    if(userLedMarkTick<1000)return ;
    userLedMarkTick = 0;
    if(userAlarmFlag==1)
    {
        userAlarmFlag = 2;
        userLedBlinkState = 0;
        userNightOn();
    }
    userLedBlinkState ^= 0xFF;
    LED_Write(userLedBlinkState);
    if(userLedBlinkState) userBeepOn();
     else userBeepOff();
}

uint8 uBackValueStart=0;
uint8 uBackValueDst=0;
uint8 uBackChangeFlag = 0;
uint8 uBackChangeEndFlag = 1;
uint8 uBackChangeCnt=0;
void userBackLedChangeFunc(void)
{
    if(uBackChangeEndFlag) return ;
    uBackChangeCnt++;
    if(uBackChangeCnt < 3) return ;
    uBackChangeCnt = 0;
    if(uBackChangeFlag)
    {
        uBackValueStart++;
        if(uBackValueStart > uBackValueDst)
        {
            uBackChangeEndFlag = 1;
            uBackValueStart = uBackValueDst;
        }
        userBacklightModifyFunc(uBackValueStart);
    }
    else
    {
        uBackValueStart--;
        if(uBackValueStart < uBackValueDst)
        {
            uBackChangeEndFlag = 1;
            uBackValueStart = uBackValueDst;
        }
        userBacklightModifyFunc(uBackValueStart);
    }
}
void userBackLedOperateFunc(unsigned char uValue)
{
    uBackValueDst = uValue;
    uBackChangeEndFlag = 0;
    if(uBackValueDst > uBackValueStart)
    {
        uBackChangeFlag = 1;
    }
    else
    {
        uBackChangeFlag = 0;
    }
}

void userBackLedSetFunc(unsigned char uLightSensorLevelValue)
{
    unsigned char userLedLevelNow = 0;
    if(uLightSensorLevelValue == 0xFF) 
    {
        userBackLedOperateFunc(userLedLevelMax);
        return ;
    }
    if(uLightSensorLevelValue == 0x00) 
    {
        userBackLedOperateFunc(userLedLevelMinL);
        return ;
    }
    userLedLevelNow = (userLedLevelMax - userLedLevelMin)* uLightSensorLevelValue / BACK_LIGHT_lEVEL_MAX;
    userLedLevelNow = userLedLevelMin + userLedLevelNow;
    userBackLedOperateFunc(userLedLevelNow);
}
